﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chowpoo.Sql;
using CourseSystem.Core.Entities;
using CourseSystem.Core.SqlServerDAL;

namespace CourseSystem.Core.BLL
{
    public class UrlStatBLL : CommonBLL<UrlStatInfo, UrlStatDAL>
    {
        public static IQueryable<UrlStatInfo> GetList(int collegeId, int departId, int subjectId, int yearId, int classId, string uid = "", string userName = "")
        {
            var re = GetList();
            re = re.Where(o => o.UserType == 1);
            if (collegeId != 0)
                re = re.Where(o => o.StudentInfo.ClassInfo.SubjectInfo.DepartmentInfo.CollegeId == collegeId);
            if (departId != 0)
                re = re.Where(o => o.StudentInfo.ClassInfo.SubjectInfo.DepartId == departId);
            if (subjectId != 0)
                re = re.Where(o => o.StudentInfo.ClassInfo.SubjectId == subjectId);
            if (classId != 0)
                re = re.Where(o => o.StudentInfo.ClassId == classId);
            if (yearId != 0)
                re = re.Where(o => o.StudentInfo.GradeYear == yearId);
            if (!string.IsNullOrEmpty(uid))
                re = re.Where(o => o.StudentInfo.Uid == uid);
            if (!string.IsNullOrEmpty(userName))
                re = re.Where(o => o.UserName.Contains(userName));
            return re;
        }

        public static IQueryable<UrlStatInfo> GetList(string account, string name, int type = -1)
        {
            var re = GetList();
            re = re.Where(o => o.UserType != 1);
            if (type != -1)
                re = re.Where(o => o.UserType == type);
            if (!string.IsNullOrEmpty(account))
                re = re.Where(o => o.TeacherInfo.Account == account);
            if (!string.IsNullOrEmpty(name))
                re = re.Where(o => o.UserName.Contains(name));
            return re;
        }

        public static UrlStatInfo Get(int id)
        {
            using (var dal = new UrlStatDAL())
            {
                var re = dal.SelectSingle(o => o.Id == id);
                return re;
            }
        }


        public static bool Del(int id)
        {
            using (var dal = new UrlStatDAL())
            {
                return dal.Delete(o => o.Id == id) != null;
            }
        }

    }
}
